﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

namespace Sizebox.CharacterEditor
{
	// Token: 0x0200027F RID: 639
	[RequireComponent(typeof(CharacterEditor))]
	[RequireComponent(typeof(SkeletonEdit))]
	public class WeldTool : MonoBehaviour
	{
		// Token: 0x170001A3 RID: 419
		// (get) Token: 0x06000E06 RID: 3590 RVA: 0x0006D841 File Offset: 0x0006BA41
		// (set) Token: 0x06000E07 RID: 3591 RVA: 0x0006D849 File Offset: 0x0006BA49
		public List<string> Keys { get; private set; }

		// Token: 0x06000E08 RID: 3592 RVA: 0x0006D852 File Offset: 0x0006BA52
		private void Awake()
		{
			this.Keys = new List<string>();
			this.CharacterEditor = base.GetComponent<CharacterEditor>();
			this.SkeletonEdit = base.GetComponent<SkeletonEdit>();
			this.MaterialEdit = base.GetComponent<MaterialEdit>();
		}

		// Token: 0x06000E09 RID: 3593 RVA: 0x0006D884 File Offset: 0x0006BA84
		public List<string> Weld(EntityBase obj, List<EditBone> bones)
		{
			List<string> list = new List<string>();
			if (obj.isPlayer)
			{
				return list;
			}
			this.CleanObject(obj);
			string item = this.Weld(obj, bones[0]);
			this.Keys.Add(item);
			list.Add(item);
			for (int i = 1; i < bones.Count; i++)
			{
				EntityBase entityBase = UnityEngine.Object.Instantiate<EntityBase>(obj);
				this.CleanObject(entityBase);
				item = this.Weld(entityBase, bones[i]);
				this.Keys.Add(item);
				list.Add(item);
			}
			return list;
		}

		// Token: 0x06000E0A RID: 3594 RVA: 0x0006D918 File Offset: 0x0006BB18
		private string Weld(EntityBase obj, EditBone parent)
		{
			if (obj.isPlayer)
			{
				return null;
			}
			obj.transform.parent = parent.RealTransform;
			string customKey = this.AddWeldData(obj.model, parent.Key);
			string result = this.CharacterEditor.RegisterTransform(obj.transform, parent.RealTransform, ";W;", customKey);
			this.SkeletonEdit.CreateBone(obj.transform, parent);
			if (this.MaterialEdit)
			{
				List<Material> list = new List<Material>();
				Renderer[] componentsInChildren = obj.GetComponentsInChildren<Renderer>();
				foreach (Renderer renderer in componentsInChildren)
				{
					foreach (Material item in renderer.materials)
					{
						list.Add(item);
					}
				}
				this.MaterialEdit.AddMaterials(list);
			}
			return result;
		}

		// Token: 0x06000E0B RID: 3595 RVA: 0x0006DA04 File Offset: 0x0006BC04
		public void Unweld(SkeletonEditHandle handle)
		{
			EntityBase component = handle.EditBone.RealTransform.GetComponent<EntityBase>();
			string key = handle.EditBone.Key;
			if (!this.Keys.Contains(key))
			{
				return;
			}
			if (component)
			{
				if (this.MaterialEdit)
				{
					HashSet<Material> hashSet = new HashSet<Material>();
					Renderer[] componentsInChildren = component.GetComponentsInChildren<Renderer>();
					foreach (Renderer renderer in componentsInChildren)
					{
						foreach (Material item in renderer.materials)
						{
							hashSet.Add(item);
						}
					}
					this.MaterialEdit.RemoveMaterials(hashSet);
				}
				this.RemoveWeldData(component, handle);
			}
			this.Keys.Remove(key);
		}

		// Token: 0x06000E0C RID: 3596 RVA: 0x0006DAE0 File Offset: 0x0006BCE0
		private void CleanObject(EntityBase weldee)
		{
			EntityBase component = base.GetComponent<EntityBase>();
			Behaviour[] componentsInChildren = weldee.GetComponentsInChildren<Behaviour>();
			foreach (Behaviour behaviour in componentsInChildren)
			{
				if (!this.IsIgnoredScript(behaviour))
				{
					behaviour.enabled = false;
				}
			}
			if (component.isMicro)
			{
				Collider[] componentsInChildren2 = weldee.GetComponentsInChildren<Collider>();
				foreach (Collider collider in componentsInChildren2)
				{
					if (collider as MeshCollider && !(collider as MeshCollider).convex)
					{
						collider.enabled = false;
					}
					else
					{
						collider.isTrigger = true;
					}
				}
			}
			if (weldee.isGiantess)
			{
				weldee.rbody.gameObject.SetActive(false);
			}
			else
			{
				Rigidbody componentInChildren = weldee.GetComponentInChildren<Rigidbody>();
				if (componentInChildren)
				{
					componentInChildren.isKinematic = true;
				}
			}
			if (component.isGiantess)
			{
				this.SetLayerRecursively(weldee.transform, Layers.gtsBodyLayer);
			}
			else
			{
				this.SetLayerRecursively(weldee.transform, Layers.defaultLayer);
			}
			weldee.gameObject.AddComponent<Weld>();
		}

		// Token: 0x06000E0D RID: 3597 RVA: 0x0006DC1C File Offset: 0x0006BE1C
		private bool IsIgnoredScript(Behaviour script)
		{
			return script as NetworkAnimator || script as NetworkTransform || script as NetworkIdentity || (script as Animator || script as DynamicBone || script as Destructor) || (script as SkeletonEdit || script as MaterialEdit);
		}

		// Token: 0x06000E0E RID: 3598 RVA: 0x0006DCB0 File Offset: 0x0006BEB0
		private void SetLayerRecursively(Transform inTransform, int layer)
		{
			if (inTransform.gameObject.layer != Layers.destroyerLayer)
			{
				inTransform.gameObject.layer = layer;
			}
			for (int i = 0; i < inTransform.childCount; i++)
			{
				this.SetLayerRecursively(inTransform.GetChild(i), layer);
			}
		}

		// Token: 0x06000E0F RID: 3599 RVA: 0x0006DD04 File Offset: 0x0006BF04
		private string AddWeldData(string objectName, string parentKey)
		{
			string key = WeldTool.CreateObjectKey(objectName, parentKey);
			if (!this.ObjectData.ContainsKey(key))
			{
				this.ObjectData.Add(key, new WeldData(objectName));
			}
			return this.ObjectData[key].Add();
		}

		// Token: 0x06000E10 RID: 3600 RVA: 0x0006DD50 File Offset: 0x0006BF50
		private void RemoveWeldData(EntityBase obj, SkeletonEditHandle handle)
		{
			string key = WeldTool.CreateObjectKey(obj.model, handle.EditBone.ParentBone.Key);
			string key2 = handle.EditBone.Key;
			if (!this.ObjectData.ContainsKey(key))
			{
				return;
			}
			string[] array = key2.Split(new string[]
			{
				";W;"
			}, StringSplitOptions.None);
			if (array.Length == 0)
			{
				return;
			}
			string weldKey = array[array.Length - 1];
			this.ObjectData[key].Remove(weldKey);
		}

		// Token: 0x06000E11 RID: 3601 RVA: 0x0006DDD0 File Offset: 0x0006BFD0
		private static string CreateObjectKey(string objectName, string parentKey)
		{
			return parentKey + objectName;
		}

		// Token: 0x040012FB RID: 4859
		public const string WELD_SEPERATOR = ";W;";

		// Token: 0x040012FC RID: 4860
		private CharacterEditor CharacterEditor;

		// Token: 0x040012FD RID: 4861
		private SkeletonEdit SkeletonEdit;

		// Token: 0x040012FE RID: 4862
		private MaterialEdit MaterialEdit;

		// Token: 0x04001300 RID: 4864
		private Dictionary<string, WeldData> ObjectData = new Dictionary<string, WeldData>();
	}
}
